<?php

namespace app\admin\controller;

use think\Db;

class Project extends Base
{
    public function index()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 10;
            $res = Db::name('project')->page($page, $limit)->order('id', 'desc')->select();
            foreach ($res as $index => $re) {
                $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
                $res[$index]["client"] = $cli;
            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('project')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }

    public function add_pro()
    {
        if (request()->isAjax()) {
            $data = input('');
            $prom = $data['prom'];
            $cusm = $data['cusm'];
            $des = $data['des'];
            $quli = $data['quli'];
            $client = $data['client'];
            if (empty($client)) {
                $this->error('客户不能为空');
            }
            if (empty($des)) {
                $this->error('设计师不能为空');
            }
            if (empty($quli)) {
                $this->error('质检不能为空');
            }

            if (empty($prom)) {
                $this->error('项目经理不能为空');
            }
            if (empty($cusm)) {
                $this->error('客户经理不能为空');
            }
            $data['project_manager_id'] = $prom['id'];
            $data['customer_manager_id'] = $cusm['id'];
            $data['designer_id'] = $des['id'];
            $data['qualityl_id'] = $quli['id'];
            $data['create_time'] = time();
            unset($data['file']);
            unset($data['prom']);
            unset($data['cusm']);
            unset($data["des"]);
            unset($data["quli"]);
            unset($data["client"]);
            $data['tuzhi'] = json_encode($data['tuzhi']);
            // dump($data);die;


            $id = Db::name('project')->insertGetId($data);//直接使用 insertGetId 方法新增数据并返回主键值

            $res = ["project_id" => "", "user_id" => "", 'name' => "",'phone'=>""];
            foreach ($client as $index => $re) {
                $res["user_id"] = $re["id"];
                $res["project_id"] = $id;
                $res["name"] = $re["name"];
                $res["phone"]=$re["phone"];
                Db::name("project_client")->insert($res);
            }
            $mid = Db::name("project_client")->getLastInsID();
            if ($id && $mid) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        } else {
            return $this->fetch();
        }
    }

    public function del_project_all()
    {
        $ids = input('ids/a');
        foreach ($ids as $id) {
            $re = Db::name('project')->where('id', $id)->delete();
        }
        $this->success('操作成功');


    }

    public function edit_project()
    {
        if (request()->isAjax()) {
            $data = input('');
            $prom = $data['prom'];
            $cusm = $data['cusm'];
            $des = $data['des'];
            $quli = $data['quli'];
            $client = $data['client'];
            if (empty($client)) {
                $this->error('客户不能为空');
            }
            if (empty($des)) {
                $this->error('设计师不能为空');
            }
            if (empty($quli)) {
                $this->error('质检不能为空');
            }

            if (empty($prom)) {
                $this->error('项目经理不能为空');
            }
            if (empty($cusm)) {
                $this->error('客户经理不能为空');
            }
            Db::name("project_client")->where('project_id', $data['id'])->delete();
            $res = ["project_id" => "", "user_id" => "", "name" => ""];
            foreach ($client as $index => $re) {
                $res["user_id"] = $re["id"];
                $res["project_id"] = $data['id'];
                $res["name"] = $re["name"];
                Db::name("project_client")->insert($res);
            }
            $data['project_manager_id'] = $prom['id'];
            $data['customer_manager_id'] = $cusm['id'];
            $data['designer_id'] = $des['id'];
            $data['qualityl_id'] = $quli['id'];
            $data['create_time'] = time();
            unset($data['file']);
            unset($data['prom']);
            unset($data['cusm']);
            unset($data["des"]);
            unset($data["quli"]);
            unset($data["client"]);

            $data['tuzhi'] = json_encode($data['tuzhi']);

            $res = Db::name('project')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }

            $this->assign('edit_pro', url('edit_project', ['id' => $id]));

            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";

            $res = Db::name('project')->find($id);

            //施工图纸
            $this->assign('qn_domain', $qiniu_config['domain']);
            $tuzhi = $this->tuzhi($res['tuzhi']);
            $this->assign('tuzhi', $tuzhi);


            $res['project_picture2'] = $qiniu_config['domain'] . '/' . $res['project_picture'] . $q_thumb;
            $cli = Db::name('project_client')->field('user_id,name')->where('project_id', $id)->select();
            foreach ($cli as $index => $re) {
                $cli[$index]['id'] = $re['user_id'];
            }
            $c = json_encode($cli);


            $pm = Db::name('user')->where('id', $res['project_manager_id'])->find();
            $res['pm'] = $pm['truename'];
            if (!$pm['avatar']) {
                $pm['avatar'] = '0';
            } else {
                $pm['avatar'] = $qiniu_config['domain'] . '/' . $pm['avatar'] . $q_thumb;
            }
            $p = json_encode($pm);

            $am = Db::name('user')->where('id', $res['customer_manager_id'])->find();
            $res['am'] = $am['truename'];
            if (!$am['avatar']) {
                $am['avatar'] = '0';
            } else {
                $am['avatar'] = $qiniu_config['domain'] . '/' . $am['avatar'] . $q_thumb;
            }
            $a = json_encode($am);

            $ds = Db::name('user')->where('id', $res['designer_id'])->find();
            $res['ds'] = $ds['truename'];
            if (!$ds['avatar']) {
                $ds['avatar'] = '0';
            } else {
                $ds['avatar'] = $qiniu_config['domain'] . '/' . $ds['avatar'] . $q_thumb;
            }
            $d = json_encode($ds);

            $qc = Db::name('user')->where('id', $res['qualityl_id'])->find();
            $res['qc'] = $qc['truename'];
            if (!$qc['avatar']) {
                $qc['avatar'] = '0';
            } else {
                $qc['avatar'] = $qiniu_config['domain'] . '/' . $qc['avatar'] . $q_thumb;
            }
            $q = json_encode($qc);


            $this->assign('item', $res);
            $this->assign('p', $p);
            $this->assign('a', $a);
            $this->assign('d', $d);
            $this->assign('q', $q);
            $this->assign('c', $c);
            return $this->fetch();
        }

    }

    /**
     * 专门处理一下图纸
     * @param array $tuzhi
     * @return array|mixed
     */
    protected function tuzhi($tuzhi = [])
    {
        $defult_img = '/res/img/thumb.png';
        $tuzhi_data = [];
        $tuzhi_data['biaoji'] = $defult_img;
        $tuzhi_data['celiang'] = $defult_img;
        $tuzhi_data['chaigai'] = $defult_img;
        $tuzhi_data['buju'] = $defult_img;
        $tuzhi_data['shiyi'] = $defult_img;
        $tuzhi_data['dingmian'] = $defult_img;
        $tuzhi_data['dingmian_chichun'] = $defult_img;
        $tuzhi_data['kaiguan'] = $defult_img;
        $tuzhi_data['qiangmian'] = $defult_img;
        $tuzhi_data['suoyin'] = $defult_img;
        $tuzhi_data['limian'] = $defult_img;
        $tuzhi_data['paomian'] = $defult_img;

        if (!$tuzhi) {
            return $tuzhi_data;
        } else {


            $tuzhi = json_decode($tuzhi, true);
            foreach ($tuzhi_data as $index => $tuzhi_datum) {
                if ($tuzhi[$index]) {
                    $tuzhi_data[$index] = $tuzhi[$index];
                }
            }

        }
        return $tuzhi_data;
    }


    public function stage()
    {
        $pid = input('id');
        if (!$pid) {
            $this->error('id 异常');
        }


        $this->project_info($pid);
        return $this->fetch();

    }


    /**
     * 工程详情
     * @return mixed
     */
    public function stage_detail()
    {
        $id = input('id');
        if (!$id) {
            $this->error('id 异常');
        }
        $stage=Db::name('project_stage')->where('id', $id)->find();
        $this->project_info($stage['project_id'],false);
        $this->assign('stage',$stage);


        $qiniu_config = cmf_get_option('qiniu');
        $this->assign('qn_domain', $qiniu_config['domain']);

        $tuzhi_data = [];
        $tuzhi_data['biaoji'] = '平面及建筑标记图';
        $tuzhi_data['celiang'] = '原始尺寸测量图';
        $tuzhi_data['chaigai'] = '墙体拆改图';
        $tuzhi_data['buju'] = '平面布局图';
        $tuzhi_data['shiyi'] = '地面材质示意图';
        $tuzhi_data['dingmian'] = '顶面天花布局图';
        $tuzhi_data['dingmian_chichun'] = '顶面天花尺寸图';
        $tuzhi_data['kaiguan'] = '开关面板示意图';
        $tuzhi_data['qiangmian'] = '墙面装饰布局图';
        $tuzhi_data['suoyin'] = '平面索引图';
        $tuzhi_data['limian'] = '立面图';
        $tuzhi_data['paomian'] = '剖面图';

        $tuzhi_arr=[];
        if($stage['tuzhi']){
            $tuzhi = json_decode($stage['tuzhi'], true);
            foreach ($tuzhi as $index => $item) {
                    $t=[];
                    $key=array_keys($item)[0];
                    $t['name']=$tuzhi_data[$key];
                    $t['src']=$item[$key];
                    $tuzhi_arr[]=$t;
            }
        }
        $this->assign('tuzhi2',$tuzhi_arr);


        return $this->fetch();

    }


    /**
     * 项目基本信息
     * @param $id
     * @param bool $has_tuzhi 是否显示图纸
     */
    public function project_info($id,$has_tuzhi=true)
    {
        $res = Db::name('project')->where('id', $id)->find();

        $cli = Db::name('project_client')->field("name")->where('project_id', $res['id'])->select();
        $res["client"] = $cli;

        $pm = Db::name('user')->where('id', $res['project_manager_id'])->find();
        $res['pm'] = $pm['truename'];

        $am = Db::name('user')->where('id', $res['customer_manager_id'])->find();
        $res['am'] = $am['truename'];

        $ds = Db::name('user')->where('id', $res['designer_id'])->find();
        $res['ds'] = $ds['truename'];

        $qc = Db::name('user')->where('id', $res['qualityl_id'])->find();
        $res['qc'] = $qc['truename'];



        if($has_tuzhi){
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            //施工图纸
            $this->assign('qn_domain', $qiniu_config['domain']);
            $tuzhi = $this->tuzhi($res['tuzhi']);
            $this->assign('tuzhi', $tuzhi);
        }


        $this->assign('res', $res);

    }


    /**
     * 获取指定项目所有的工程
     */
    public function get_stage()
    {
        $pid = input('id');
        if (!$pid) {
            $this->error('id 异常');
        }
        $re = Db::name('project_stage')->where('project_id', $pid)->where('is_delete',0)
            ->order('id', 'desc')->select();
        $this->success('ok', '', $re);
    }

    public function del_stage()
    {
        $id = input('id');
        $re=Db::name('project_stage')->where('id', $id)->update(["is_delete"=>1]);
        $this->success('操作成功');
    }

    public function add_stage()
    {
        if (request()->isAjax()) {
            $data = input('');
            if ($data['s'] == 'add') {
                $data['status'] = 0;
            }
//            if ($data['s'] == 'send') {
//                $data['status'] = 1;
//            }
//            $material = [];
//            if (!empty($data["material"])) {
//                $material = $data["material"];
//            }
            $data['create_time'] = time();
            $data['tuzhi'] = json_encode($data['tuzhi']);
//            unset($data["material"]);
            unset($data['s']);
            unset($data['file']);
//            unset($data['wt']);

            $id = Db::name('project_stage')->insertGetId($data);//直接使用 insertGetId 方法新增数据并返回主键值

//            $res = ["num" => "", "project_stage_id" => "", "goods_id" => "", "order_code" => ""];
//            foreach ($material as $index => $re) {
//                $res["num"] = $re["num"];
//                $res["project_stage_id"] = $id;
//                $res["goods_id"] = $re["id"];
//                $res["order_code"] = cmf_get_order_sn();
//
//                Db::name("project_stage_material")->insert($res);
//            }


//            $mid = Db::name("project_stage_material")->getLastInsID();
            if ($id) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        } else {
            $id = input("id");
            $res = Db::name('project')->find($id);
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            // $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            //施工图纸
            $this->assign('qn_domain', $qiniu_config['domain']);
            $tuzhi = $this->tuzhi($res['tuzhi']);
            $this->assign('tuzhi', $tuzhi);

            $this->biaozhun();


            return $this->fetch();
        }
    }

    /**
     * 施工标准
     */
    protected function biaozhun()
    {

        $res = Db::name('project_biaozhun')->select();
        $this->assign('biaozhun', $res);

    }


    public function edit_stage()
    {

        if (request()->isAjax()) {
            $data = input('');
            $material = [];
            if (!empty($data["material"])) {
                $material = $data["material"];
            }
            $rel = ["num" => "", "project_stage_id" => "", "goods_id" => "", "order_code" => ""];
            Db::name("project_stage_material")->where('project_stage_id', $data['id'])->delete();
            foreach ($material as $index => $re) {
                $rel["num"] = $re["num"];
                $rel["project_stage_id"] = $data['id'];
                $rel["goods_id"] = $re["id"];
                $rel["order_code"] = cmf_get_order_sn();
                Db::name("project_stage_material")->insert($rel);
            }
            unset($data['file']);
            unset($data["material"]);
            unset($data['wt']);
            $res = Db::name('project_stage')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');


        } else {
            //阶段id
            $id = input('id');
            if (!$id) {
                $this->error('id 异常');
            }
            $res = Db::name('project_stage')->find($id);
            $rel = Db::name('project_stage_material')->where('project_stage_id', $res['id'])->select();
            foreach ($rel as $index => $re) {
                $good = Db::name("goods")->where('id', $re['goods_id'])->find();
                $rel[$index]['name'] = $good['name'];
                $rel[$index]['id'] = $good['id'];
            }
            $s = json_encode($rel);
            $this->assign('s', $s);
            $this->assign('item', $res);
            return $this->fetch();
        }
    }

    public function stage_status()
    {
        if (request()->isAjax()) {
            $data = input("");
            $res = Db::name('project_stage')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        }

    }

    public function select_pm()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_pm', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('user')->where('is_pm', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function select_am()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_am', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('user')->where('is_am', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function select_ds()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_ds', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('user')->where('is_ds', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function select_qc()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_qc', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('user')->where('is_qc', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function select_client()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $count = Db::name('user')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function edit_select_pm()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_pm', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $id = input("id");
            $p = "";
            if ($id) {
                $res = Db::name('project')->find($id);
                $pm = Db::name('user')->where('id', $res['project_manager_id'])->find();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                if (!$pm['avatar']) {
                    $pm['avatar'] = '0';
                } else {
                    $pm['avatar'] = $qiniu_config['domain'] . '/' . $pm['avatar'] . $q_thumb;
                }
                $p = json_encode($pm);

            }
            $this->assign("p", $p);
            $count = Db::name('user')->where('is_pm', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function edit_select_am()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_am', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $id = input("id");
            $a = "";
            if ($id) {
                $res = Db::name('project')->find($id);
                $am = Db::name('user')->where('id', $res['customer_manager_id'])->find();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                if (!$am['avatar']) {
                    $am['avatar'] = '0';
                } else {
                    $am['avatar'] = $qiniu_config['domain'] . '/' . $am['avatar'] . $q_thumb;
                }
                $a = json_encode($am);

            }
            $this->assign("a", $a);
            $count = Db::name('user')->where('is_am', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function edit_select_ds()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_ds', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $id = input("id");
            $d = "";
            if ($id) {
                $res = Db::name('project')->find($id);
                $ds = Db::name('user')->where('id', $res['designer_id'])->find();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                if (!$ds['avatar']) {
                    $ds['avatar'] = '0';
                } else {
                    $ds['avatar'] = $qiniu_config['domain'] . '/' . $ds['avatar'] . $q_thumb;
                }
                $d = json_encode($ds);

            }
            $this->assign("d", $d);
            $count = Db::name('user')->where('is_ds', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function edit_select_qc()
    {
        if (request()->isAjax()) {

            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('user')->where('is_qc', 1)->page($page, $limit)->order('id', 'desc')->select();
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar']) {
                    $res[$index]['avatar'] = '0';
                } else {
                    $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $id = input("id");
            $q = "";
            if ($id) {
                $res = Db::name('project')->find($id);
                $qc = Db::name('user')->where('id', $res['qualityl_id'])->find();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!60x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                if (!$qc['avatar']) {
                    $qc['avatar'] = '0';
                } else {
                    $qc['avatar'] = $qiniu_config['domain'] . '/' . $qc['avatar'] . $q_thumb;
                }
                $q = json_encode($qc);
            }
            $this->assign("q", $q);
            $count = Db::name('user')->where('is_qc', 1)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function select_material()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('goods')->page($page, $limit)->select();
            foreach ($res as $index => $re) {
                $pinpais = Db::name('goods_pinpai')->where('id', $re['pinpai_id'])->find();
                $pinpai = $pinpais['pinpai'];
                $res[$index]['pinpai'] = $pinpai;
            }
            if ($res) {
                $this->success('ok', '', $res);
            }
            $this->error("暂无此数据");

        } else {
            $count = Db::name('goods')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }

    public function edit_select_material()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('goods')->page($page, $limit)->select();
            foreach ($res as $index => $re) {
                $pinpais = Db::name('goods_pinpai')->where('id', $re['pinpai_id'])->find();
                $pinpai = $pinpais['pinpai'];
                $res[$index]['pinpai'] = $pinpai;
            }
            if ($res) {
                $this->success('ok', '', $res);
            }
            $this->error("暂无此数据");

        } else {
            $sid = input('id');
            $rel = Db::name('project_stage_material')->where('project_stage_id', $sid)->select();
            foreach ($rel as $index => $re) {
                $good = Db::name("goods")->where('id', $re['goods_id'])->find();
                $rel[$index]['name'] = $good['name'];
                $rel[$index]['id'] = $good['id'];
            }
            $s = json_encode($rel);
            $this->assign('s', $s);

            $count = Db::name('goods')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }

    public function select_material_list()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('goods')->page($page, $limit)->select();
            foreach ($res as $index => $re) {
                $pinpais = Db::name('goods_pinpai')->where('id', $re['pinpai_id'])->find();
                $pinpai = $pinpais['pinpai'];
                $res[$index]['pinpai'] = $pinpai;
            }
            if ($res) {
                $this->success('ok', '', $res);
            }
            $this->error("暂无此数据");

        } else {
            $sid = input('id');
            $rel = Db::name('project_stage_material')->where('project_stage_id', $sid)->select();
            foreach ($rel as $index => $re) {
                $good = Db::name("goods")->where('id', $re['goods_id'])->find();
                $rel[$index]['name'] = $good['name'];
                $rel[$index]['id'] = $good['id'];
            }
            $s = json_encode($rel);
            $this->assign('s', $s);

            $count = Db::name('goods')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }

    public function select_material_list1()
    {
        if (request()->isAjax()) {
            $s = input('');
            $id = $s['id'];
            $rel = ["num" => "", "project_stage_id" => "", "goods_id" => "", "order_code" => ""];
            Db::name("project_stage_material")->where('project_stage_id', $id)->delete();
            foreach ($s["data"] as $index => $re) {
                $rel["num"] = $re["num"];
                $rel["project_stage_id"] = $id;
                $rel["goods_id"] = $re["id"];
                $rel["order_code"] = cmf_get_order_sn();
                Db::name("project_stage_material")->insert($rel);
                //生成一个订单，向商家发消息
            }
            $mid = Db::name("project_material")->getLastInsID();
            if ($mid) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }

    }


    public function select()
    {

        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 6;
        $flag = input('flag');
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }
        if ($flag == 5) {
            $res = Db::name('user')
                ->where('phone LIKE :key1 OR truename LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->page($page, $limit)
                ->select();
            //根据关键字返回 相应数据
            $total = Db::name('user')
                ->where('phone LIKE :key1 OR truename LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->count();
        } else {
            $item = '';
            if ($flag == 1) {
                $item = "is_pm";
            }
            if ($flag == 2) {
                $item = "is_am";
            }
            if ($flag == 3) {
                $item = "is_ds";
            }
            if ($flag == 4) {
                $item = "is_qc";
            }
            $res = Db::name('user')
                ->where('phone LIKE :key1 OR truename LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->where($item, 1)
                ->page($page, $limit)
                ->select();
            //根据关键字返回 相应数据
            $total = Db::name('user')
                ->where('phone LIKE :key1 OR truename LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
                ->where($item, 1)
                ->count();
        }
        $qiniu_config = cmf_get_option('qiniu');
        // 七牛样式处理
        $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
        foreach ($res as $index => $re) {
            if (!$re['avatar']) {
                $res[$index]['avatar'] = '0';
            } else {
                $res[$index]['avatar'] = $qiniu_config['domain'] . '/' . $re['avatar'] . $q_thumb;
            }

        }
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

    }

    public function select_good()
    {

        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $data = input('');

            $goods_name = $data['goods_name'];
            $shanghu = $data['shanghu'];
            $fenlei = $data['fenlei'];

            //单独处理商家
            if ($shanghu) {

            } else {

            }


            $where_str = '';
            if ($goods_name) {
                $where_str .= " name like '%$goods_name%' ";
            }
            if ($fenlei) {
                $where_str .= "1 and fenlei like '%$fenlei%'";
            }


            $res = Db::name('goods')
                ->where($where_str)
                ->page($page, $limit)
                ->select();
            foreach ($res as $index => $re) {
                $shop_name = Db::name('user_biz')->where('uid', $re['shop_uid'])->value('shop_name');
                $res[$index]['shop_name'] = $shop_name;

                $pinpais = Db::name('goods_pinpai')->where('id', $re['pinpai_id'])->value('pinpai');
                $res[$index]['pinpai'] = $pinpais;
            }
            //根据关键字返回 相应数据
            $total = Db::name('goods')
                ->where($where_str)
                ->count();
            return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);


        }

    }


    /**
     *
     * @param $name
     */
    protected function search_biz($name)
    {

    }


    public function bid()
    {

        if (request()->isAjax()) {
            $sid = input('id');
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 6;
            $res = Db::name('project_bid_record')->where('stage_id', $sid)
                ->page($page, $limit)->order('id', 'desc')->select();
            foreach ($res as $index => $re) {
                $worker = Db::name('user_worker')->where('uid', $re['worker_id'])->find();
                $res[$index]['avatar2'] = $worker['avatar2'];
                $res[$index]['gongzhong'] = $worker['gongzhong'];
            }
            foreach ($res as $index => $re) {
                $user = Db::name('user')->where('id', $re['worker_id'])->find();
                $res[$index]['truename'] = $user['truename'];
                $res[$index]['phone'] = $user['phone'];
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar2']) {
                    $res[$index]['avatar2'] = '0';
                } else {
                    $res[$index]['avatar2'] = $qiniu_config['domain'] . '/' . $re['avatar2'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        } else {
            $sid = input('id');
            $types=Db::name("project_worker_type")->where('stage_id',$sid)->select();

            $this->assign('types',$types);

            $count = Db::name('project_bid_record')->where('stage_id', $sid)->count();

            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    public function bid_status()
    {
        if (request()->isAjax()) {
            $data = input("");
            $res = Db::name('project_bid_record')->where('id', $data['id'])->update($data);
            if ($res === false) {
                $this->success('操作失败');
            }
            $this->success('操作成功');

        }

    }

    public function bid_worker()
    {
        if (request()->isAjax()) {
            $s = input('');
            $id = $s['id'];
            $res = Db::name("project_stage")->where('id', $id)->update($s);
            if ($res) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        }

    }

    public function stage_log()
    {
        if (request()->isAjax()) {
            $sid = input('id');
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 2;
            $res = Db::name('project_stage_log')->where('stage_id', $sid)
                ->page($page, $limit)->order('log_time', 'desc')->select();

            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!320x200r/gravity/Center/crop/320x200/quality/100/interlace/0";
            for ($i=0;$i<count($res);$i++) {
                $pictures = Db::name('project_stage_log_picture')->where('stage_log_id', $res[$i]['id'])
                    ->field('log_picture')
                    ->select();
                foreach ($pictures as $index => $pic) {
                    $pictures[$index]['log_picture'] = $qiniu_config['domain'] . '/' . $pic['log_picture'] . $q_thumb;
                }
                $res[$i]['pictures'] = $pictures;
            }
            $this->success('ok', '', $res);

        } else {
            $sid = input('id');
            $count = Db::name('project_stage_log')->where('stage_id', $sid)->count();
            $this->assign('count', $count);
            return $this->fetch();
        }
    }

    /**
     * 施工标准
     */
    public function biaozhun_list()
    {
        if (request()->isAjax()) {

            $res = Db::name('project_biaozhun')->order('id', 'desc')->select();
            $this->success('ok', '', $res);

        } else {
            return $this->fetch();
        }
    }

    /**
     * 施工标准 详细
     */
    public function biaozhun_detail()
    {

        if (request()->isAjax()) {

            if (!request()->has('id')) {
                $this->error('no id');
            }
            $data = input('');
            $id = input('id');
            unset($data['id']);
            $res = Db::name('project_biaozhun')->where('id', $id)->update($data);
            $this->success('保存成功');

        } else {
            if (!request()->has('id')) {
                $this->error('no id');
            }
            $id = input('id');
            $res = Db::name('project_biaozhun')->where('id', $id)->find();
            $this->assign('item', $res);

            return $this->fetch();
        }


    }


    /**
     * 施工标准 新增
     */
    public function biaozhun_add()
    {

        if (!request()->has('title')) {
            $this->error('缺少名称');
        }
        $data = input('');
        Db::name('project_biaozhun')->insertGetId($data);
        $this->success('操作成功');

    }

    /**
     * 施工标准 删除
     */
    public function biaozhun_del()
    {

        if (!request()->has('id')) {
            $this->error('no id');
        }
        $id = input('id');

        Db::name('project_biaozhun')->where('id', $id)->delete();
        $this->success('操作成功');

    }

    public function project_worker()
    {
        $sid=input("id");
        $s=Db::name("project_stage")->where('id',$sid)->find();
//        dump($s).die();
        $workers=[];
        if($s['worker_id']){
            $worker_id= explode(",", $s['worker_id']);
            $worker=["avatar2"=>"","truename"=>"","gongzhong"=>"","phone"=>""];
            foreach ($worker_id as $index => $id) {
                $u=Db::name("user")->where('id',$id)->find();
                $worker["truename"]=$u['truename'];
                $worker["phone"]=$u['phone'];
                $w=Db::name("user_worker")->where('uid',$id)->find();
                $qiniu_config = cmf_get_option('qiniu');
                // 七牛样式处理
                $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
                if (!$w['avatar2']) {
                    $w['avatar2'] = '0';
                } else {
                    $w['avatar2'] = $qiniu_config['domain'] . '/' . $w['avatar2'] . $q_thumb;
                }
                $worker["avatar2"]=$w['avatar2'];
                $worker["gongzhong"]=$w['gongzhong'];

                $workers[$index]=$worker;
            }
        }

        $types=Db::name("project_worker_type")->where('stage_id',$sid)->select();
        $this->assign('workers',$workers);
        $this->assign('types',$types);
        return $this->fetch();
    }
    public function worker_type(){
        if (request()->isAjax()) {
            $d= input('');
            $res = Db::name('project_stage')->where('id', $d['id'])->update(["status"=>1]);
            $rel = ["stage_id" => "", "type" => "", "pricet" => ""];
            Db::name("project_worker_type")->where('stage_id',$d['id'])->delete();
            foreach ($d["data"] as $index => $re) {
                $rel["stage_id"] = $d['id'];
                $rel["type"] = $re["type"];
                $rel["pricet"] = $re["pricet"];
                Db::name("project_worker_type")->insert($rel);
            }
            $mid = Db::name("project_worker_type")->getLastInsID();
            if ($mid&&$res) {
                $this->success('操作成功');
            }
            $this->error('操作失败');

        } else {
            $id = input('id');
            $res = Db::name('project_worker_type')->where('stage_id', $id)->select();
            $s=json_encode($res);
            $this->assign('s', $s);
            return $this->fetch();
        }



    }
    public function worker_type_list(){
            $id = input('id');
            $res = Db::name('project_worker_type')->where('stage_id', $id)->select();
            $this->assign('res', $res);
            return $this->fetch();

    }

    public function project_stage()
    {
        if (request()->isAjax()) {
            $page = input('page') ? input('page') : 1;
            $limit = input('limit') ? input('limit') : 8;
            $res = Db::name('project_stage')->page($page, $limit)->where('is_delete',0)
                ->order('id', 'desc')->select();
            foreach ($res as $index => $re) {
                $pro=Db::name('project')->where('id', $re['project_id'])->find();
                $cli = Db::name('project_client')->field("name")->where('project_id', $pro['id'])->select();
                $res[$index]["client"] = $cli;
                $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
                $res[$index]['pm'] = $pm['truename'];
            }
            $this->success('ok', '', $res);
        } else {
            $count = Db::name('project_stage')->count();
            $this->assign('count', $count);
            return $this->fetch();
        }

    }
    public function yanshou()
    {

        if (request()->isAjax()) {
            $sid = input('id');
            $res = Db::name('project_ys_record')->where('stage_id', $sid)
               ->order('id', 'desc')->select();
            foreach ($res as $index => $re) {
                $worker = Db::name('user_worker')->where('uid', $re['worker_id'])->find();
                $res[$index]['avatar2'] = $worker['avatar2'];
                $res[$index]['gongzhong'] = $worker['gongzhong'];
            }
            foreach ($res as $index => $re) {
                $user = Db::name('user')->where('id', $re['worker_id'])->find();
                $res[$index]['truename'] = $user['truename'];
                $res[$index]['phone'] = $user['phone'];
            }
            $qiniu_config = cmf_get_option('qiniu');
            // 七牛样式处理
            $q_thumb = "?imageMogr2/auto-orient/thumbnail/!120x120r/gravity/Center/crop/120x120/quality/100/interlace/0";
            foreach ($res as $index => $re) {
                if (!$re['avatar2']) {
                    $res[$index]['avatar2'] = '0';
                } else {
                    $res[$index]['avatar2'] = $qiniu_config['domain'] . '/' . $re['avatar2'] . $q_thumb;
                }

            }
            $this->success('ok', '', $res);

        }
        else{
            return $this->fetch();
        }
    }
    public function worker_price()
    {
        if (request()->isAjax()) {
                Db::startTrans();
                try {
                    $flag = true;
                    $yid=input("yid");
                    $wid=input("wid");
                    $price = input("price");
                    $res = Db::name('project_ys_record')->where('id', $yid)->update(["status"=>2]);
                    if(!$res){
                        $msg = '状态更新失败';
                        $flag = false;
                    }
                    $record = Db::name('record')->insert(['record' =>+(float)$price, 'worker_id' =>$wid]);
                    if (!$record) {
                        $msg = '插入交易记录失败';
                        $flag = false;

                    }
                    $salary=Db::name('user')->where('id', $wid)->find();
                    $salary['money'] = (float)$salary['money']+(float)$price;
                        $jisuan = Db::name('user')->where('id', $wid)->update(['money' => $salary['money']]);
                        if (!$jisuan) {
                            $msg = '余额更新失败';
                            $flag = false;
                        }

                    if (!$flag) {
                        // 事务回滚
                        Db::rollback();
                        $this->error($msg);
                    }else{
                        Db::commit();
                        $this->success("操作成功");

                    }


                } catch (Exception $exception) {
                    Db::rollback();
                    $this->error("操作失败");
                }

        }else{
            $sid=input("sid");
            $type=input("type");
            $price=Db::name("project_worker_type")->where("stage_id",$sid)->where("type",$type)->find();
            $this->assign("price",$price);
            return $this->fetch();
        }
    }
    public function project_add_stage()
    {
        if (request()->isAjax()) {
            $data = input('');
            if ($data['s'] == 'add') {
                $data['status'] = 0;
            }$data['create_time'] = time();
            $data['tuzhi'] = json_encode($data['tuzhi']);

            unset($data['s']);
            unset($data['file']);
            $id = Db::name('project_stage')->insertGetId($data);//直接使用 insertGetId 方法新增数据并返回主键值

            if ($id) {
                $this->success('操作成功');
            }
            $this->error('操作失败');
        } else {
            $this->biaozhun();
            return $this->fetch();
        }
    }
    public function project_search()
    {
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 10;
        $key = trim(input('keyword'));
        if (!$key) {
            $this->error('没有内容');
        }
        $res=Db::name('project_client')->join('iw_project','iw_project_client.project_id=iw_project.id')->page($page,$limit)
            ->where('phone LIKE :key1 OR name LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])->order('iw_project.id', 'desc')
            ->select();
        foreach ($res as $index => $re) {
            $cli = Db::name('project_client')->field("name")->where('project_id', $re['id'])->select();
            $res[$index]["client"] = $cli;
        }
        $total = Db::name('project_client')->join('iw_project','iw_project_client.project_id=iw_project.id')
            ->where('phone LIKE :key1 OR name LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])
            ->count();
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

    }

    public function stage_search()
    {
        $page = input('page') ? input('page') : 1;
        $limit = input('limit') ? input('limit') : 8;
        $type=trim(input('type'));
        $title=trim(input('title'));
        $key=trim(input('keyword'));
        $total=0;
        $res=[];
        if (!$key&&!$type&&!$title) {
            $this->error('没有内容');
        }
        if($type){
            $res=Db::name('project_worker_type')->join('iw_project_stage','iw_project_worker_type.stage_id=iw_project_stage.id')->page($page,$limit)
                ->where('type LIKE :key ', ['key' => "%$type%"]) ->where('is_delete',0) ->order('iw_project_stage.id', 'desc')
                ->select();
            foreach ($res as $index => $re) {
                $pro=Db::name('project')->where('id', $re['project_id'])->find();
                $cli = Db::name('project_client')->field("name")->where('project_id', $pro['id'])->select();
                $res[$index]["client"] = $cli;
                $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
                $res[$index]['pm'] = $pm['truename'];
            }
            $total = Db::name('project_worker_type')->join('iw_project_stage','iw_project_worker_type.stage_id=iw_project_stage.id')
                ->where('is_delete',0)
                ->where('type LIKE :key ', ['key' => "%$type%"])
                ->count();
        }
        else{
            if($title&&!$key){
                $res=Db::name('project_stage')->page($page,$limit)
                    ->where('title LIKE :key ', ['key' => "%$title%"]) ->where('is_delete',0) ->order('id', 'desc')
                    ->select();
                foreach ($res as $index => $re) {
                    $pro=Db::name('project')->where('id', $re['project_id'])->find();
                    $cli = Db::name('project_client')->field("name")->where('project_id', $pro['id'])->select();
                    $res[$index]["client"] = $cli;
                    $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
                    $res[$index]['pm'] = $pm['truename'];
                }
                $total =Db::name('project_stage')->page($page,$limit)
                    ->where('title LIKE :key ', ['key' => "%$title%"]) ->where('is_delete',0) ->order('id', 'desc')
                    ->count();
            }
            if($key&&!$title){
                $datas=Db::name('project_client')->field('project_id')->where('phone LIKE :key1 OR name LIKE :key ', ['key1' => "%$key%", 'key' => "%$key%"])->select();
                $ids=[];
                foreach ($datas as $index => $i) {
                    $ids[$index] = $i['project_id'];
                }
                $str = implode(",", $ids);
                $res=Db::name("project_stage")->where('project_id','in',$str)->page($page,$limit)->where('is_delete',0) ->order('id', 'desc')->select();
                foreach ($res as $index => $re) {
                    $pro=Db::name('project')->where('id', $re['project_id'])->find();
                    $cli = Db::name('project_client')->field("name")->where('project_id', $pro['id'])->select();
                    $res[$index]["client"] = $cli;
                    $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
                    $res[$index]['pm'] = $pm['truename'];
                }
                $total =Db::name("project_stage")->where('is_delete',0)->where('project_id','in',$str)->count();
            }
            if($key&&$title){
                $res=Db::name('project_stage')->join('iw_project_client','iw_project_stage.project_id=iw_project_client.project_id')->page($page,$limit)
                   ->where('phone LIKE :key1 OR name LIKE :key AND title LIKE :key2', ['key1' => "%$key%", 'key' => "%$key%",'key2'=>"%$title%"])
                    ->where('is_delete',0) ->order('iw_project_stage.id', 'desc')
                    ->select();
                foreach ($res as $index => $re) {
                    $pro=Db::name('project')->where('id', $re['project_id'])->find();
                    $cli = Db::name('project_client')->field("name")->where('project_id', $pro['id'])->select();
                    $res[$index]["client"] = $cli;
                    $pm = Db::name('user')->where('id', $pro['project_manager_id'])->find();
                    $res[$index]['pm'] = $pm['truename'];
                }
                $total=Db::name('project_stage')->join('iw_project_client','iw_project_stage.project_id=iw_project_client.project_id')
                    ->where('phone LIKE :key1 OR name LIKE :key  AND title LIKE :key2', ['key1' => "%$key%", 'key' => "%$key%",'key2'=>"%$title%"])
                    ->where('is_delete',0)
                    ->select();
            }
        }
        return json(['code' => 1, 'msg' => 'ok', 'total' => $total, 'data' => $res]);

    }

}